package com.qfedu.provider.controller;

import com.qfedu.config.SystemConfig;
import com.qfedu.param.DynamicAdd;
import com.qfedu.provider.entity.DbDynamics;
import com.qfedu.provider.service.DbDynamicsService;
import com.qfedu.provider.service.DbUserService;
import com.qfedu.provider.util.MyMinioUtil;
import com.qfedu.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Administrator
 * @date 2024/11/23 上午11:25
 */
@RestController
@RequestMapping("/server/dynamic")
@Tag(name = "动态管理")
@RequiredArgsConstructor
public class DynamicController {

    private final DbUserService dbUserService;
    private final DbDynamicsService dbDynamicsService;

    @Operation(summary = "查询所有动态")
    @GetMapping("/findAll")
    public R find() {
        return dbDynamicsService.queryAll();
    }

    @Operation(summary = "根据id查询动态")
    @GetMapping("/findById")
    public R findById(Integer id) {
        return dbDynamicsService.queryById(id);
    }

    //查询个人动态
    @Operation(summary = "查询个人动态")
    @GetMapping("/findAllByUserId")
    public R findAllByUserId(String userId) {
        return dbDynamicsService.queryAllDynamicByUserId(Integer.parseInt(userId));
    }

    @Operation(summary = "发布动态")
    @PostMapping("/publish")
    public R publish(@RequestBody DynamicAdd dynamicAdd,HttpServletRequest request) {
        return dbDynamicsService.addDynamic(dynamicAdd,request.getHeader(SystemConfig.IP));
    }

    @PostMapping("uploadImg")
    @Operation(summary = "封面上传")
    public R upload(@RequestPart("file") MultipartFile file,Integer did,HttpServletRequest request) throws IOException {
        return dbDynamicsService.upload(file,did,request.getHeader(SystemConfig.IP));
    }

    @Operation(summary = "删除动态")
    @PostMapping("/delete")
    public R delete(Integer did,HttpServletRequest request) {
        return dbDynamicsService.removeDynamic(did,request.getHeader(SystemConfig.IP));
    }

}
